﻿using System;
using System.Globalization;
using System.Xml.Linq;
using FarseerGames.FarseerPhysics.Mathematics;

namespace FreeGoo.LevelClasses.LevelParts
{
    public class FactoryLevelPart : LevelPart
    {
        public override string Name { get { return "Factory"; } }
        public override void AddToEngineFromXml(Level level, XElement element)
        {
            Vector2 position = element.Attribute("Position").ToVector2();
            Vector2 size = element.Attribute("Size").ToVector2();
            int count = element.Attribute("Count").ToInt(1);

            Random random = new Random();
            Vector2 particlePosition = new Vector2(0, 0);

            foreach (XElement subElement in element.Elements())
            {
                int tempCount = subElement.Attribute("Count").ToInt(count);

                for (int i = 0; i < tempCount; i++)
                {
                    string res =
                        string.Format(
                            CultureInfo.InvariantCulture,
                            "{0},{1}",
                            position.X - (size.X / 2) + ((float)random.NextDouble() * size.X),
                            position.Y - (size.Y / 2) + ((float)random.NextDouble()) * size.Y);

                    subElement.SetAttributeValue("Position", res);
                    level.AddXmlLevelPart(subElement);
                }
            }
        }
    }
}
